<template>
  <div class="auth-guard">
    <div class="auth-container">
      <div class="auth-card">
        <div class="auth-header">
          <h1>系统授权验证</h1>
          <p>请输入授权码以访问系统</p>
        </div>
        
        <form @submit.prevent="handleSubmit" class="auth-form">
          <div class="input-group">
            <label for="authCode">授权码</label>
            <input
              id="authCode"
              v-model="inputCode"
              type="password"
              placeholder="请输入授权码"
              :class="{ error: hasError }"
              @input="clearError"
            />
            <div v-if="hasError" class="error-message">
              {{ errorMessage }}
            </div>
          </div>
          
          <button type="submit" class="auth-button" :disabled="!inputCode.trim()">
            验证授权
          </button>
        </form>
        
        <div class="auth-footer">
          <p>请联系管理员获取授权码</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'

const emit = defineEmits<{
  authorized: []
}>()

const inputCode = ref('')
const hasError = ref(false)
const errorMessage = ref('')

const handleSubmit = () => {
  const correctCode = import.meta.env.VITE_AUTH_CODE
  
  if (!inputCode.value.trim()) {
    showError('请输入授权码')
    return
  }
  
  if (inputCode.value.trim() === correctCode) {
    // 授权成功，保存到 localStorage
    localStorage.setItem('auth_verified', 'true')
    localStorage.setItem('auth_timestamp', Date.now().toString())
    emit('authorized')
  } else {
    showError('授权码错误，请重新输入')
    inputCode.value = ''
  }
}

const showError = (message: string) => {
  hasError.value = true
  errorMessage.value = message
}

const clearError = () => {
  hasError.value = false
  errorMessage.value = ''
}
</script>

<style scoped>
.auth-guard {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
}

.auth-container {
  width: 100%;
  max-width: 400px;
  padding: 20px;
}

.auth-card {
  background: white;
  border-radius: 12px;
  padding: 40px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  text-align: center;
}

.auth-header h1 {
  color: #333;
  margin: 0 0 8px 0;
  font-size: 24px;
  font-weight: 600;
}

.auth-header p {
  color: #666;
  margin: 0 0 32px 0;
  font-size: 14px;
}

.auth-form {
  text-align: left;
}

.input-group {
  margin-bottom: 24px;
}

.input-group label {
  display: block;
  margin-bottom: 8px;
  color: #333;
  font-weight: 500;
  font-size: 14px;
}

.input-group input {
  width: 100%;
  padding: 12px 16px;
  border: 2px solid #e1e5e9;
  border-radius: 8px;
  font-size: 16px;
  transition: border-color 0.2s;
  box-sizing: border-box;
}

.input-group input:focus {
  outline: none;
  border-color: #667eea;
}

.input-group input.error {
  border-color: #e74c3c;
}

.error-message {
  color: #e74c3c;
  font-size: 12px;
  margin-top: 4px;
}

.auth-button {
  width: 100%;
  padding: 12px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: opacity 0.2s;
}

.auth-button:hover:not(:disabled) {
  opacity: 0.9;
}

.auth-button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.auth-footer {
  margin-top: 24px;
  padding-top: 24px;
  border-top: 1px solid #e1e5e9;
}

.auth-footer p {
  color: #999;
  font-size: 12px;
  margin: 0;
}

@media (max-width: 480px) {
  .auth-container {
    padding: 16px;
  }
  
  .auth-card {
    padding: 24px;
  }
}
</style>